﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TopNewLib.TopNews;

namespace TopNewsDesktop
{
    /// <summary>
    /// TopNewControl.xaml 的交互逻辑
    /// </summary>
    public partial class TopNewControl : UserControl
    {
        Timer tm = new Timer();
        BaseTop tb = null;
        ObservableCollection<TopNewItem> memberData = new ObservableCollection<TopNewItem>();

        string typeNameNews = "baidu";

        public string TypeNameNews
        {
            get { return typeNameNews; }
            set { typeNameNews = value; }
        }

        public TopNewControl()
        {
            InitializeComponent();
        }

        public TopNewControl(string typeName)
        {
            InitializeComponent();
            
        }

        private void InitForm(string typeName)
        {
            tm.Elapsed += tm_Elapsed;
            tm.Interval = 5000;
            switch (typeName)
            {
                case "baidu":
                    tb = new BaiduTopClass();
                    TitleLable.Content = "百度实时热搜";
                    break;
                case "weibo":
                    tb = new WeiBoTopClass();
                    TitleLable.Content = "微博实时热搜";
                    break;
                case "sougouwx":
                    tb = new SouGouWXTopClass();
                    TitleLable.Content = "微信实时热搜";
                    break;
                default:
                    break;
            }
        }

        private void tm_Elapsed(object sender, ElapsedEventArgs e)
        {
            ObservableCollection<TopNewItem> memberData1 = new ObservableCollection<TopNewItem>(tb.GetCurTopNews());
            Dispatcher.Invoke(new Action(() =>
            {
                memberData.Clear();

                foreach (var item in memberData1)
                {
                    memberData.Add(item);
                }
            }));
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            InitForm(TypeNameNews);

            memberData = new ObservableCollection<TopNewItem>(tb.GetCurTopNews());
            dataGrid.DataContext = memberData;

            tm.Start();
        }

        private void DG_Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            string title = ((Hyperlink)e.OriginalSource).NavigateUri.ToString();
            foreach (var item in memberData)
            {
                if (item.Title == title)
                {
                    System.Diagnostics.Process.Start(item.Url);
                }
            }
        }
    }
}
